<?
if( !defined( 'MODULE_FILE' ) ) {
	die( "cant access file directly!" );
}

$mid 	= ( ( true == isset( $_GET['mid'] ) && true == valStr( $_GET['mid'] ) ) ? $_GET['mid'] : NULL );
$smid 	= ( ( true == isset( $_GET['smid'] ) && true == valStr( $_GET['smid'] ) ) ? $_GET['smid'] : NULL );

autoLoader();

$objBaseWard 	= new BaseWards();
$objWards 		= new Wards();
$objWardTypes 	= new WardTypes();
$objPatients 	= new Patients();
$objDepartments = new Departments();
$objDoctors 	= new Doctors();

$arrobjWardCatagories = new WardCatagories();
$arrobjWardCatagories = $arrobjWardCatagories->fetchAllWardCatagories();

?>
<html>
<head>
<?php require_once 'commonJS.php'; ?>
</head>
<body>
	<div style="margin-left:12px; margin-top: 10px;">
		<p>
			<a class="btn btn-outline btn-primary btn-xs" id="act" href="modules.php?file=wards&mid=<? echo $mid;?>&smid=<? echo $smid;?>" >Add New Ward</a>
			<a class="btn btn-outline btn-primary btn-xs" id="active" href="modules.php?file=view_wards&type=view&mid=<? echo $mid;?>&smid=<? echo $smid;?>" >View All Wards</a>&nbsp;
		</p>
	</div>
<div class="col-lg-8">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-bell fa-fw"></i>Add Wards Details</div>
<div class="panel-body">
	<form METHOD="POST" action="modules.php?file=insert_wards&type=insert&mid=<? echo $mid; ?>&smid=<? echo $smid; ?>" id="formSubmit" name="formSubmit" >
				<?php
				$intWardId 			= NULL;
				$intWardCatagoryId 	= NULL;
				$intWardTypeId 		= NULL;
				$intRoomNumber 		= NULL;
				$intRate 			= NULL;
				$intIsAvaliable 	= NULL;
				$strMoveOutDate 	= NULL;
				$intReservationId 	= NULL;
				$intReportingDoctorId = NULL;
				$intPatientId 		= NULL;
				$intDepartmentId 	= NULL;
				$strFacilities 		= NULL;
				
				$intReferancePatientId = NULL;
				
				if( true == isset( $_GET['ward_id'] ) && true == valStr( $_GET['ward_id'] ) ) {
					$intWardId = $_GET['ward_id'];
					
					$objBaseWard = $objWards->fetchWardsById( $intWardId );
					
					if( true == valObj( $objBaseWard, 'BaseWards' ) ) {
						$intWardId 				= $objBaseWard->getId();
						$intWardTypeId 			= $objBaseWard->getWardTypeId();
						$intRoomNumber 			= $objBaseWard->getRoomNumber();
						$intRate 				= $objBaseWard->getRate();
						$intIsAvaliable 		= $objBaseWard->getIsAvaliable();
						$strMoveOutDate 		= $objBaseWard->getMoveOutDate();
						$intReservationId 		= $objBaseWard->getReservationId();
						$intReportingDoctorId 	= $objBaseWard->getReportingDoctorId();
						$intPatientId 			= $objBaseWard->getPatientId();
						$intDepartmentId 		= $objBaseWard->getDepartmentId();
						$strFacilities 			= $objBaseWard->getFacilities();
						
						$objWardType = new WardTypes();
						$objWardType = $objWardType->fetchWardTypesById( $objBaseWard->getWardtypeId() );
						
						if( true == valObj( $objWardType, 'BaseWardTypes' ) ) {
							$objWardCatagory = new WardCatagories();
							$objWardCatagory = $objWardCatagory->fetchWardCatagoriesById( $objWardType->getWardCatagoryId() );
							
							$intWardCatagoryId = ( true == valObj( $objWardCatagory, 'BaseWardCatagories' ) ) ? $objWardCatagory->getId() : NULL;
						}
					}
				} else if( true == valObj( $objBaseWard, 'BaseWards' ) ) {
					$intWardId = $objBaseWard->fetchNextId();
				}
			?>
		<table width="80%" cellspacing="0" cellpadding="3">
			<tr>
				<td height="10">
					<input id="ward_id" Name="ward_id" type="text" class="" style="width:100px; display: none;" readonly="readonly" value="<?php echo $intWardId; ?>">
					Ward Catagory:-
				</td>
				<td>
					<select name="ward_catagory_id" id="ward_catagory_id">
						<option id="ward_catagory_id_null" value="">--Select Ward Catagory--</option>
					<?php
						if( true == valArr( $arrobjWardCatagories ) ) {
							foreach( $arrobjWardCatagories as $objWardCatagory ) {
								$strSelected = ( $intWardCatagoryId == $objWardCatagory->getId() ) ? 'selected="selected"' : NULL;
								echo '<option ' . $strSelected . ' id="' . $objWardCatagory->getId() . '" value="' . $objWardCatagory->getId() . '">' . $objWardCatagory->getName() . '</option>';
							}
						}
					?>
					</select>
				</td>
			</tr>
			<tr valign="top">
				<td class="mandatory" width="220">Ward Type</td>
				<td>
					<select class="validate[required]" name="ward_type" id="ward_type" style="width:202px;">
						<option value=""> -- please choose -- </option>
						<?php
							//$arrobjWardTypes = $objWardTypes->fetchAllWardTypes();
							
							if( true == valStr( $arrobjWardTypes ) ) {
								foreach( $arrobjWardTypes as $objWardType ) {
									$strSelected = ( $intWardTypeId == $objWardType->getId() ) ? 'selected="selected"' : NULL;
									echo '<option ' . $strSelected . ' value=' . $objWardType->getId() . '>' . $objWardType->getName() . '</option>';
								}
							}
						?>
					</select>
				</td>
			</tr>
			<tr valign="top">
				<td class="mandatory" width="220">Room Number</td>
				<td><input id="room_number" Name="room_number" type="text" class="validate[required]" style="width:202px;" value="<?php echo $intRoomNumber; ?>"></td>
			</tr>
			<tr><td height="10" colspan="2"></td></tr>
			<tr><td height="10" colspan="2"></td></tr>
			<tr valign="top">
				<td class="mandatory" width="220">Rate</td>
				<td><input id="rate" Name="rate" type="text" class="validate[required]" style="width:202px;" value="<?php echo $intRate; ?>"></td>
			</tr>
			<tr><td height="10" colspan="2"></td></tr>
			<tr valign="top">
				<td class="mandatory" width="220">Is Available</td>
				<td><input id="is_avaliable" Name="is_avaliable" type="text" style="width:202px;" value="<?php echo $intIsAvaliable; ?>"></td>
			</tr>
			<tr><td height="10" colspan="2"></td></tr>
			<tr valign="top">
				<td class="mandatory" width="220">Move Out Date</td>
				<td><input id="move_out_date" Name="move_out_date" type="text" style="width:202px;" value="<?php echo ( true == valStr( $strMoveOutDate ) && strtotime( date( 'd-m-Y' ) ) <= strtotime( date( 'd-m-Y', strtotime( $strMoveOutDate ) ) ) ) ? date( 'd-m-Y', strtotime( $strMoveOutDate ) ) : NULL; ?>"></td>
			</tr>
			<tr><td height="10" colspan="2"></td></tr>
			<tr valign="top">
				<td class="mandatory" width="220">Reservation Id</td>
				<td><input id="reservation_id" Name="reservation_id" type="text" style="width:202px;" value="<?php echo ( 'NULL' != $intReservationId ) ? $intReservationId : NULL; ?>"></td>
			</tr>
			<tr><td height="10" colspan="2"></td></tr>
			<tr valign="top">
				<td class="mandatory" width="220">Reporting Doctor</td>
				<td>
				<select class="validate[required]" name="reporting_doctor_id" id="reporting_doctor_id" style="width:202px;">
					<option value=""> -- please choose -- </option>
					<?php
						$arrobjDoctors = $objDoctors->fetchAllDoctors();
						
						if( true == valStr( $arrobjDoctors ) ) {
							foreach( $arrobjDoctors as $objDoctor ) {
								echo '<option' . ( ( $intReportingDoctorId == $objDoctor->getId() ) ? ' selected' : '' ) . ' value=' . $objDoctor->getId() . '>' . $objDoctor->getFirstName() . '&nbsp;'. $objDoctor->getLastName() . '</option>';
							}
						}
					?>
				</select>
			  </td>
			</tr>
			<tr><td height="10" colspan="2"></td></tr>
			<tr valign="top">
				<td class="mandatory" width="220">Patient ID</td>
				<td>
				<select name="patient_id" id="patient_id" style="width:202px;">
					<option value=""> -- please choose -- </option>
					<?php
							$arrobjPatients = $objPatients->fetchAllPatients();
							
							if( true == valStr( $arrobjPatients ) ) {
								foreach( $arrobjPatients as $objPatient ) {
									echo '<option' . ( ( $intPatientId == $objPatient->getId() ) ? ' selected' : '' ) . ' value=' . $objPatient->getId() . '>' . $objPatient->getFirstName() . '&nbsp;'. $objPatient->getLastName() . '</option>';
								}
							}
					?>
				</select>
			  </td>
			</tr>
			<tr><td height="10" colspan="2"></td></tr>
			<tr valign="top">
				<td class="mandatory" width="220">Department</td>
				<td>
				<select class="validate[required]" name="department_id" id="department_id" style="width:202px;">
				    <option value=""> -- please choose -- </option>
					<?php
							$arrobjDepartments = $objDepartments->fetchAllDepartments();
							
							if( true == valStr( $arrobjDepartments ) ) {
								foreach( $arrobjDepartments as $objDepartment ) {
									echo '<option' . ( ( $intDepartmentId == $objDepartment->getId() ) ? ' selected' : '' ) . ' value=' . $objDepartment->getId() . '>' . $objDepartment->getName() . '</option>';
								}
							}
					?>	
				</select>
			  </td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			<tr valign="top">
				<td class="mandatory" width="220">Facilities</td>
				<td><input id="facilities" Name="facilities" type="text" class="validate[required]" style="width:202px;" value="<?php echo $strFacilities; ?>"></td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			<tr valign="top">
				<td class="mandatory" width="220">&nbsp;</td>
				<td>
				<?php
						if( true == isset( $_GET['ward_id'] ) && true == valStr( $_GET['ward_id'] ) ) {
							echo "<input type=hidden name=operation value=update>";
							echo "<input type=submit value=UPDATE id=Submit name=Submit>";
						} else {
							echo "<input type=hidden name=operation value=insert>";
							echo "<input type=submit value=SUBMIT id=Submit name=Submit>";
						}
					?>
				</td> 				
			</tr>
		</table>
	</form>
</div>
</div>
</div>	
</body>
<script>
	$('#ward_catagory_id').change( function() {
		
		var intWardCatagoryId =	$(this).val();

		var getParams={ ward_type_id:intWardCatagoryId };

		$.getJSON( 'modules.php?file=fetch_list_items&type=fetch&action=ward_types&ward_catagory_id=' + intWardCatagoryId, getParams,
		function( data ) {
			strWardTypeListData = "<option id='' value=''>--Select Ward Type--</option>";
			
			if( data != 'not found::' ) {
				for( var i = 0; i < data.length; i++ ) {
					data[i]
					strWardTypeListData += '<option id="' + data[i]['id'] + '" value="' + data[i]['id'] + '">' + data[i]['name'] + '</option>';
				}

				$('#ward_type').html( strWardTypeListData );
			}
		});
	});
</script>
</html>